package com.example.itcacp.repository.mybatis;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.itcacp.entity.Collect;
import com.example.itcacp.entity.Like;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 收藏Mapper层
 * */

@Repository
public interface CollectMapper extends BaseMapper<Collect> {
    /**
     * 查询对应内容收藏数
     * COUNT（字段）: 计数
     * GROUP_CONCAT(字段) : 合并
     * */
    @Select( "SELECT COUNT(*) FROM pc_collection WHERE " +
            "target_id = #{contentId} AND target_type = #{contentType}" )
    Long findCollect (Long contentId, Like.ContentType contentType);


    @Select("SELECT COUNT(*) FROM pc_collection " +
            "WHERE target_id = #{contentId} AND target_type = 'PROJECT'")
    Long findCollectByProjectId(Long contentId);
    /**
     * 查询用户是否已收藏
     */
    @Select("SELECT COUNT(*) FROM pc_collection " +
            "WHERE user_id = #{userId} AND target_id = #{targetId} AND target_type = #{targetType}")
    Integer isCollect(@Param("userId") Long userId,
                    @Param("targetId") Long targetId,
                    @Param("targetType") Like.ContentType targetType);

    /**
     * 查询收藏记录
     * */
    @Select ( "SELECT * FROM pc_collection " +
            "WHERE user_id = #{userId} AND target_id = #{targetId} AND target_type = #{targetType}" )
    Collect findCollectId ( Long userId, Long targetId, Like.ContentType targetType);

    @Select ( "SELECT * FROM itcacp.pc_collection WHERE user_id = #{userId}" )
    List<Collect> getCollectsByUserId(@Param("userId") Long userId);
}
